package com.example.newstudent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.newstudent.dao.optional_courseDao;
import com.example.newstudent.dao.studentDao;
import com.example.newstudent.dao.userDao;
import com.example.newstudent.entity.optional_course;
import com.example.newstudent.entity.student;
import com.example.newstudent.entity.user;
import com.example.newstudent.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class userImpl implements
        userService {
    @Autowired
    userDao userDao;
    @Autowired
    studentDao studentDao;
    @Autowired
    optional_courseDao optional_courseDao;

    @Override
    public user Login(user user) {
        QueryWrapper<user> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",user.getAccount()).eq("pin",user.getPin());
        return userDao.selectOne(queryWrapper);
    }

    @Override
    public List<student> userSelect() {
        return studentDao.selectList(null);
    }

    @Override
    public void userDelete(student student) {
        UpdateWrapper<student> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",student.getId());
        studentDao.delete(updateWrapper);
    }

    @Override
    public user updatemessage(user user) {
        return null;
    }
    @Override
    public void addOptional(optional_course optional_course) {
        String[] strings = optional_course.getWeeks().split(",");
        System.out.println(strings[0]+strings[1]);
        int weekEnd = Integer.parseInt(strings[1]);
        int weekStar = Integer.parseInt(strings[0]);
        for (int i = weekStar; i <= weekEnd; i++) {
            optional_course.setWeeks(String.valueOf(i));
            optional_courseDao.insert(optional_course);
        }
    }

    @Override
    public void deleteOptional(String name) {
        QueryWrapper<optional_course> optional_courseQueryWrapper = new QueryWrapper<>();
        optional_courseQueryWrapper.eq("name",name);
        optional_courseDao.delete(optional_courseQueryWrapper);
    }

    @Override
    public List<optional_course> optional(String name) {
        if (name == null) {
            return optional_courseDao.selectList(null);
        }else {
            QueryWrapper<optional_course> optional_courseQueryWrapper = new QueryWrapper<>();
            optional_courseQueryWrapper.eq("name", name);
            return optional_courseDao.selectList(optional_courseQueryWrapper);
        }
    }
}
